#include "OrderFile.h"

//构造函数
OrderFile::OrderFile()
{
    ifstream ifs;
    ifs.open(ORDER_FILE, ios::in);

    string date;    //日期
    string interval;    //时间段
    string stuId;   //学生编号
    string stuName;     //学生姓名
    string roomId;      //机房编号
    string status;      //预约状态

    size = 0;   //记录条数

    while (ifs >> date && ifs >> interval && ifs >> stuId &&
        ifs >> stuName && ifs >> roomId && ifs >> status)
    {
        string key;
        string value;
        map<string, string>m;

        //截取星期
        int pos = date.find(":");
        if (pos != -1)
        {
            key = date.substr(0, pos);
            value = date.substr(pos + 1, date.size() - pos - 1);

            m.insert(make_pair(key, value));
        }

        //截取时间段
        pos = interval.find(":");
        if (pos != -1)
        {
            key = interval.substr(0, pos);
            value = interval.substr(pos + 1, interval.size() - pos - 1);

            m.insert(make_pair(key, value));
        }

        //截取学号
        pos = stuId.find(":");
        if (pos != -1)
        {
            key = stuId.substr(0, pos);
            value = stuId.substr(pos + 1, stuId.size() - pos - 1);

            m.insert(make_pair(key, value));
        }

        //截取姓名
        pos = stuName.find(":");
        if (pos != -1)
        {
            key = stuName.substr(0, pos);
            value = stuName.substr(pos + 1, stuName.size() - pos - 1);

            m.insert(make_pair(key, value));
        }

        //截取机房号
        pos = roomId.find(":");
        if (pos != -1)
        {
            key = roomId.substr(0, pos);
            value = roomId.substr(pos + 1, roomId.size() - pos - 1);

            m.insert(make_pair(key, value));
        }

        //截取预约状态
        pos = status.find(":");
        if (pos != -1)
        {
            key = status.substr(0, pos);
            value = status.substr(pos + 1, status.size() - pos - 1);

            m.insert(make_pair(key, value));
        }

        //将小map容器放入到大的map容器中
        orderData.insert(make_pair(size, m));
        size++;

    }
    ifs.close();

    //测试最大map容器
    // for (map<int, map<string, string> >::iterator it = orderData.begin();it != orderData.end();it++)
    // {
    //     cout << "key = " << it->first << " value = " << endl;
        
    //     for (map<string, string>::iterator mit = (*it).second.begin();mit != (*it).second.end();mit++)
    //     {
    //         cout << "key = " << mit->first <<
    //             " value = " << mit->second << endl;
    //     }
    //     cout << endl;
    // }

}

//更新预约记录
void OrderFile::updateOrder()
{
    if (size == 0)
    {
        return; //预约记录0条，直接return
    }

    ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
    for (int i = 0;i < size;i++)
    {
        ofs << "date:" << orderData[i]["date"] << " ";
        ofs << "interval:" << orderData[i]["interval"] << " ";
        ofs << "stuId:" << orderData[i]["stuId"] << " ";
        ofs << "stuName:" << orderData[i]["stuName"] << " ";
        ofs << "roomId:" << orderData[i]["roomId"] << " ";
        ofs << "status:" << orderData[i]["status"] << endl;
    }

    ofs.close();
}